package rz.socket;

/*
 * socket
 */
import java.net.*;
import java.io.*;

public class Service {
    private ServerSocket ss;
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    
    public Service() {
        try {
            ss = new ServerSocket(20000);
            while(true) {
                socket = ss.accept();
                String remoteIP = socket.getInetAddress().getHostAddress();
                String remotePort = ":"+socket.getLocalPort();
                System.out.println("A client come in! IP: " + remoteIP + remotePort);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line = in.readLine();
                System.out.println("Client send is: "+line);
                out = new PrintWriter(socket.getOutputStream(), true); //println、printf 或 format 方法将刷新输出缓冲区
                out.println("Your Message Received!");
                //in.close();
                //out.close();
                //socket.close();
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        new Service();
    }

}




